package ltd.hellotrik.common.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 在SpringBoot中通过注解注册的方式简单的使用Filter
 *
 * @author chengxi
 */
@WebFilter(urlPatterns = "/*", filterName = "myfilter")
public class HttpFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("Filter初始化中");
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		System.out.println("开始进行过滤处理");
		// 调用该方法后，表示过滤器经过原来的url请求处理方法
		SysContent.setRequest((HttpServletRequest) servletRequest);
		SysContent.setResponse((HttpServletResponse) servletResponse);
		filterChain.doFilter(servletRequest, servletResponse);

	}

	@Override
	public void destroy() {
		System.out.println("Filter销毁中");
	}
}
